package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostItemDao;
import ysq.xyg.host.model.po.HostArea;
import ysq.xyg.host.model.po.HostItem;
import ysq.xyg.host.service.HostAreaService;
import ysq.xyg.host.service.HostCityService;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostItemVo;
import ysq.xyg.host.service.HostItemService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 房产模块-小区信息service实现
 *
 * @author shengQiang yu
 * @date 2019-7-1 15:25:38
 */
@Service("hostItemService")
public class HostItemServiceImpl extends BaseServiceImpl<HostItem> implements HostItemService {
    @Resource
    private HostAreaService hostAreaService;
    @Resource
    private HostItemDao hostItemDao;

    private HostItem hostItem;

    @Autowired
    public void setBaseDao(HostItemDao hostItemDao) {
        this.baseDao = hostItemDao;
    }


    /**
     * 分页查询
     *
     * @param hostItemVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public ReturnEntity page(HostItemVo hostItemVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<HostItem> list = listLeft(hostItemVo);
        PageInfo<HostItem> page=new PageInfo<HostItem>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 获取集合
     *
     * @param hostItemVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public List<HostItem> listLeft(HostItemVo hostItemVo){
        return hostItemDao.listLeft(hostItemVo);
    }
    /**
     * 修改
     *
     * @param hostItemVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public ReturnEntity addOrUpdate(HostItemVo hostItemVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostItemVo.getId())){
            hostItem=new HostItem(currentUser);
            isUpate=false;
        }else{
            hostItem=selectByPrimaryKey(hostItemVo.getId());
            if(ObjectUtil.isNull(hostItem)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostItem.setUpdateTime(new Date());
            hostItem.setUpdateUser(currentUser.getId());
        }

        hostItem.setLatitude(hostItemVo.getLatitude());
        hostItem.setLongitude(hostItemVo.getLongitude());
        hostItem.setHostType(hostItemVo.getHostType());
        hostItem.setName(hostItemVo.getName());
        hostItem.setCoverImage(hostItemVo.getCoverImage());
        hostItem.setImages(hostItemVo.getImages());
        hostItem.setPrice(hostItemVo.getPrice());
        hostItem.setMinApartmentArea(hostItemVo.getMinApartmentArea());
        hostItem.setMaxApartmentArea(hostItemVo.getMaxApartmentArea());
        hostItem.setMinRooms(hostItemVo.getMinRooms());
        hostItem.setMaxRooms(hostItemVo.getMaxRooms());
        hostItem.setAreaId(hostItemVo.getAreaId());
        hostItem.setAddress(hostItemVo.getAddress());
        hostItem.setShareType(hostItemVo.getShareType());
        hostItem.setShareMoney(hostItemVo.getShareMoney());
        hostItem.setUserId(hostItemVo.getUserId());
        hostItem.setDescription(hostItemVo.getDescription());
        hostItem.setContent(hostItemVo.getContent());
        hostItem.setTag(hostItemVo.getTag());
        hostItem.setHotSaleState(hostItemVo.getHotSaleState());
        hostItem.setSort(hostItemVo.getSort());
        if(StrUtil.isNotEmpty(hostItem.getAreaId())){
            HostArea hostArea=hostAreaService.selectByPrimaryKey(hostItem.getAreaId());
            hostItem.setCityId(hostArea.getCityId());
        }
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostItem);
        }else{
            num=insert(hostItem);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostItem hostItem=selectByPrimaryKey(id);
        if(hostItem!=null){
            hostItem.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostItem.setUpdateTime(new Date());
            hostItem.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostItem);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostItemVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public List<HostItem> acceptList(HostItemVo hostItemVo){
        Example example = new Example(HostItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostItemVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostItemVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostItemVo.getCityId())){
            criteria.andEqualTo("cityId",hostItemVo.getCityId());
        }
        example.setOrderByClause(" update_time desc");
        return hostItemDao.selectByExample(example);
    }
    /**
     * 获取单个
     *
     * @param hostItemVo 对象
     * @return List<HostItem> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 15:25:38
     */
    public HostItem selectLeftObject(HostItemVo hostItemVo){
        List<HostItem> hostItemList=hostItemDao.listLeft(hostItemVo);
        if(hostItemList!=null&&hostItemList.size()>0){
            HostItem hostItem=hostItemList.get(0);
            if(StrUtil.isNotEmpty(hostItem.getImages())){
                hostItem.setImageList(hostItem.getImages().split(","));
            }
            if(StrUtil.isNotEmpty(hostItem.getTag())){
                hostItem.setTagList(hostItem.getTag().split(","));
            }
            return hostItem;
        }
        return null;
    }
}
